//
//  homeViewController.m
//  footBoll
//
//  Created by zhoubaitong on 2017/10/10.
//  Copyright © 2017年 cckv. All rights reserved.
//

#import "homeViewController.h"
#import "ViewController.h"
#import "selectBarrier.h"
#import "AnimationWordView.h"
#import "UIImage+expand.h"
#import "UIView+extension.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

#define kTipnH 185

@interface homeViewController ()<selectBarrierViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *musicBtn;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@property (strong, nonatomic) selectBarrier *BarrierView;
@property(strong, nonatomic)AnimationWordView *animationWordView;
@property (strong, nonatomic) UIButton *bgView;

@end

@implementation homeViewController
//是否可以旋转

- (BOOL)shouldAutorotate

{
    
    return YES;
    
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    BOOL haveData = [[NSUserDefaults standardUserDefaults]boolForKey:@"haveData"];
    if (!haveData) {
        //    NSMutableArray *dataArray = [NSMutableArray array];
        for (int i = 0; i< 12; i++) {
            dataModel *model = [dataModel new];
            model.levelIndex = i;
            model.pass = NO;
            if (i == 0) {
                model.pass = YES;
            }
            [model save];
            //        [dataArray addObject:model];
        }
        //    [[NSUserDefaults standardUserDefaults]setObject:dataArray forKey:@"dataArray"];
        //    [[NSUserDefaults standardUserDefaults]synchronize];
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"haveData"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    

    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.playBtn.layer.cornerRadius = 20;
    self.playBtn.layer.masksToBounds = YES;
    [self.playBtn setTitle:NSLocalizedString(@"play game", nil) forState:UIControlStateNormal];
    
    [self.view addSubview:self.animationWordView];
    self.animationWordView.animationWord = NSLocalizedString(@"Super Soccer", nil);
    self.animationWordView.animationDuration = 10;
    self.animationWordView.wordColor = [UIColor blueColor];
    self.animationWordView.centerX = self.view.centerX;
    
    [self.view bringSubviewToFront:self.musicBtn];
    
    [self.musicBtn setImage:[[UIImage imageNamed:@"voice0.png"]imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.musicBtn setImage:[[UIImage imageNamed:@"voice.png"]imageByTintColor:[UIColor whiteColor]] forState:UIControlStateSelected];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(startGame:) name:@"chooseLevel" object:nil];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.animationWordView startAnimation];

}

-(void)clickBackButton
{
    [self bgClick];
}

-(void)chooseChannelWithIndex:(int)index
{
    [UIView animateWithDuration:0.25 animations:^{
        self.BarrierView.frame = CGRectMake(100, -200, 467, kTipnH);
        self.BarrierView.centerX = self.view.centerX;
    } completion:^(BOOL finished) {
        
        [self.bgView removeFromSuperview];
        self.bgView = nil;
        [self.BarrierView removeFromSuperview];
        self.BarrierView = nil;
        
        ViewController *vc = [[ViewController alloc]init];
        vc.index = index;
        [self presentViewController:vc animated:NO completion:^{
        }];
        
    }];

}

- (IBAction)startGame:(UIButton *)sender {
    
    [self.view addSubview:self.bgView];
    
    [self.view addSubview:self.BarrierView];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.BarrierView.frame = CGRectMake(100, (kScreenH - 200)/2, 467, kTipnH);
        self.BarrierView.centerX = self.view.centerX;
    }];
}

- (IBAction)playMusic:(UIButton *)sender {
    
    sender.selected = !sender.selected;
}

- (void)bgClick
{
    [UIView animateWithDuration:0.25 animations:^{
        self.BarrierView.frame = CGRectMake(100, -200, 467, kTipnH);
        self.BarrierView.centerX = self.view.centerX;
    } completion:^(BOOL finished) {
        
        [self.bgView removeFromSuperview];
        self.bgView = nil;
        [self.BarrierView removeFromSuperview];
        self.BarrierView = nil;
    }];
}

-(selectBarrier *)BarrierView
{
    if (!_BarrierView) {
        _BarrierView = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([selectBarrier class]) owner:nil options:nil].firstObject;
        _BarrierView.frame = CGRectMake((kScreenW - 200)/2, -200, 467, kTipnH);
        _BarrierView.x = 100;
        _BarrierView.delegate = self;
        _BarrierView.centerX = self.view.centerX;
    }
    return _BarrierView;
}
-(UIButton *)bgView
{
    if (!_bgView) {
        _bgView = [[UIButton alloc]initWithFrame:self.view.bounds];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.2;
        [_bgView addTarget:self action:@selector(bgClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView;
}
- (AnimationWordView *)animationWordView
{
    if (!_animationWordView) {
        _animationWordView = [[AnimationWordView alloc]initWithFrame:CGRectMake(20, 0, kScreenW-40, 100)];
    }
    return _animationWordView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
